package com.example.redis;
import java.util.Set;

import com.baomidou.springboot.utils.SerializableUtil;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class RedisClient {

	private JedisPool jedisPool;

	/**
	 * setVExpire(设置key值，同时设置失效时间 秒
	 */
	public void set(String key, String value) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.set(key, value);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			this.close(jedis);
		}
	}

	public void set(String key, Object value) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.set(key.getBytes(), SerializableUtil.serialize(value));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			this.close(jedis);
		}
	}

	/**
	 * (存入redis数据)
	 */
	public void expire(String key, String value, Integer times) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.set(key, value);
			jedis.expire(key, times);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			this.close(jedis);
		}
	}

	public void expire(String key, Object value, Integer times) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.set(key.getBytes(), SerializableUtil.serialize(value));
			jedis.expire(key.getBytes(), times);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			this.close(jedis);
		}
	}

	/**
	 * 删除redis数据
	 */
	public void del(String key) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.del(key);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			this.close(jedis);
		}
	}

	public String get(String key) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			String s = jedis.get(key);
			return s;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			this.close(jedis);
		}
		return null;
	}

	public Object getObj(String key) {
		if (key != null) {
			Jedis jedis = null;
			try {
				jedis = jedisPool.getResource();
				byte[] s = jedis.get(key.getBytes());
				return SerializableUtil.unserialize(s);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				this.close(jedis);
			}
		}
		return null;
	}

	public Long pttl(String key) {
		if (key != null) {
			Jedis jedis = null;
			try {
				jedis = jedisPool.getResource();
				long i = jedis.pttl(key);
				return i;
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				this.close(jedis);
			}
		}
		return null;
	}

	/**
	 * 释放连接
	 * 
	 * @param jedis
	 */
	public void close(Jedis jedis) {
		if (jedis != null) {
			jedis.close();
			if (jedis.isConnected()) {
				try {
					jedis.disconnect();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 取出所有key
	 * 
	 * @return
	 */
	public Set<String> getKeys(String key) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			return jedis.keys(key + "*");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			this.close(jedis);
		}
		return null;
	}

	public JedisPool getJedisPool() {
		return jedisPool;
	}

	public void setJedisPool(JedisPool jedisPool) {
		this.jedisPool = jedisPool;
	}
}
